package com.studymaster.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.studymaster.service.User_DetailService;

@Controller
public class User_DetailConstroller {

	@Autowired
	private User_DetailService mUser_DetailService;
	
	
	@RequestMapping(value = "/updateuserheadicon.do" , method = RequestMethod.POST)
	public ModelAndView updateHeadIcon(@RequestParam("studentid") String studentid,
			@RequestParam("headicon") String headicon,HttpServletRequest request
			, ModelMap model) {
		boolean isSuccess = mUser_DetailService.UpdateHeadIcon(studentid, headicon);
		if(isSuccess){  //头像修改成功
			model.put("errorcode", "1");
			model.put("picdate", String.valueOf(System.currentTimeMillis()));
		}else{//头像修改不成功
			model.put("errorcode", "2");
		}
		
		return new ModelAndView(new MappingJacksonJsonView(), model);
		
	}
	
	@RequestMapping(value = "/downloadnewheadicon.do" , method = RequestMethod.POST)
	public ModelAndView downLoadNewHeadIcon(@RequestParam("studentid") String studentid
			,@RequestParam("picdate") String picdate,HttpServletRequest request, ModelMap model) {
		String newPicAndDate = mUser_DetailService.DownLoadNewHeadIcon(studentid,picdate);
		if(!newPicAndDate.equals("")){  //服务器的头像比本地的新，则不为空
			String str[]=newPicAndDate.split(",");
			model.put("errorcode", "1");
			model.put("headicon" , str[0]);
			model.put("picdate"  , str[1]);
		}else{//没有新头像
			model.put("errorcode", "2");
		}
		return new ModelAndView(new MappingJacksonJsonView(), model);
		
	}
	
}
